package com.cetcs.kmga.dataView.service.impl;

import com.cetc.cloud.kmga.util.DateUtils;
import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.dataView.dao.mybatis.DataManagerMapper;
import com.cetcs.kmga.dataView.entity.dataManager.CodeAndValue;
import com.cetcs.kmga.dataView.entity.dataManager.PieChart;
import com.cetcs.kmga.dataView.entity.dataManager.vo.CircularChartVo;
import com.cetcs.kmga.dataView.entity.dataManager.vo.RankOfBehaviorVo;
import com.cetcs.kmga.dataView.entity.dataManager.vo.ResourceRiskEventVo;
import com.cetcs.kmga.dataView.service.IDataManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xutao
 * @version V1.0 创建时间：2018-01-23 15:56
 *          Copyright 2018 by CETC
 */
@Service
public class DataManagerServiceImpl implements IDataManagerService {

    @Autowired
    private DataManagerMapper dataManagerMapper;

    @Override
    public String statSysSafeIndexById(String sysId) {
        //开始计算指数
        List<CodeAndValue> riskIndexList = dataManagerMapper.statResourceRiskIndexList(sysId);
        List<CodeAndValue> weightIndexList = dataManagerMapper.statResourceWeights(sysId);
        riskIndexList.forEach(p -> {
            List<CodeAndValue> singleWeight = weightIndexList.stream( ).filter(k -> p.getCode( ).equals(k.getCode( ))).collect(Collectors.toList( ));
            float wh = 0;
            if (singleWeight != null && singleWeight.size( ) > 0) {
                wh = Float.parseFloat(singleWeight.get(0).getValue( ));
            }
            float val = Float.parseFloat(p.getValue( ));
            float inVal = val * wh;

            p.setValue(String.valueOf(inVal));
        });
        Double totalIndVal = riskIndexList.stream( ).mapToDouble(p -> Double.parseDouble(p.getValue( ))).sum( );

        String riskTi = "";
        DecimalFormat df = new DecimalFormat("#0.00");
        if (totalIndVal == 0) {
            riskTi = "100";
        } else {
            totalIndVal = 100 - totalIndVal;
            riskTi = df.format(totalIndVal);
        }
        return riskTi;
    }

    @Override
    public List<RankOfBehaviorVo> findRankOfResourceAccess(String sysId) {
        Data dataMap = new Data( );
        dataMap.put("starTime", DateUtils.format(DateUtils.getTodayBefore(8), DateUtils.DATE_FORMAT_1));
        dataMap.put("endTime", DateUtils.format(DateUtils.getTodayBefore(1), DateUtils.DATE_FORMAT_1));
        dataMap.put("sysId", sysId);
        Integer total = dataManagerMapper.getAccessNumOfResourceAccess(dataMap);
        dataMap.put("total_access_num", total == null ? 1 : total);
        return dataManagerMapper.getRankOfResourceAccess(dataMap);
    }

    @Override
    public List<ResourceRiskEventVo> findNewlyRiskEvent(String sysId) {
        return dataManagerMapper.findNewlyRiskEvent(sysId);
    }

    @Override
    public CircularChartVo findRiskLevelStatistic(String sysId) {
        CircularChartVo ret = new CircularChartVo( );
        Data dataMap = new Data( );
        dataMap.put("startDate", DateUtils.format(DateUtils.getTodayBefore(8), DateUtils.DATE_FORMAT_1));
        dataMap.put("endDate", DateUtils.format(DateUtils.getTodayBefore(1), DateUtils.DATE_FORMAT_1));
        dataMap.put("sysId", sysId);
        List<PieChart> pieCharts = dataManagerMapper.findRiskLevelStatistic(dataMap);
        ret.setName("风险级别");
        if (pieCharts.size( ) > 0) {
            //数据不为空则显示动态数据
            for (PieChart pieChart : pieCharts) {
                ret.addData(pieChart.getName( ), Double.valueOf(pieChart.getData( )), pieChart.getId( ));
            }
        } else {
            //如果没有动态数据 则显示静态
//            ret.addData("低风险", 23.8, "1");
//            ret.addData("高风险", 29.76, "2");
//            ret.addData("一般风险", 46.44, "3");
        }
        return ret;
    }
}
